package com.zw.yzk.kabu.view.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.zw.yzk.kabu.R;

/**
 * Created by zhanwei on 2017/3/1.
 */

public class CommentDialog {
    private PopupWindow popupWindow;
    private Context context;
    private EditText comment;
    private TextView send;

    public CommentDialog(Context context) {
        this.context = context;
    }

    //创建popupwindow
    private void createPopupWindow(Context context) {
        if (popupWindow == null) {
            View rootView = LayoutInflater.from(context).inflate(R.layout.dialog_comment, null);
            comment = (EditText) rootView.findViewById(R.id.comment);
            send = (TextView) rootView.findViewById(R.id.send);

            send.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (!TextUtils.isEmpty(comment.getText().toString())) {
                        if (listener != null) {
                            listener.onClick(v);
                        }
                        popupWindow.dismiss();
                    }
                }
            });

            //加载对话框布局
            popupWindow = new PopupWindow(rootView, ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            //设置点击popupwindow外消失
            popupWindow.setBackgroundDrawable(new ColorDrawable());
            popupWindow.setOutsideTouchable(true);
            popupWindow.setFocusable(true);
            //设置popupwindow不被软键盘遮挡
            popupWindow.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
            popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        } else {
            comment.setText("");
        }
        comment.setFocusable(true);
        comment.setFocusableInTouchMode(true);
        comment.requestFocus();
        comment.requestFocusFromTouch();
    }

    //显示
    public void showDialog(View view) {
        createPopupWindow(context);
        popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }

    //关闭popupwindow
    public void dismiss() {
        if (popupWindow == null) {
            return;
        }
        popupWindow.dismiss();
    }

    public String getComment() {
        if(comment != null) {
            return comment.getText().toString();
        }
        return "";
    }

    private OnSendClickedListener listener;

    public interface OnSendClickedListener {
        void onClick(View v);
    }

    //设置item点击监听事件
    public void setOnSendClickedListener(OnSendClickedListener listener) {
        this.listener = listener;
    }
}
